function arrayUnique(e) {
    var t = e.concat();
    for (var n = 0; n < t.length; n++) {
        for (var r = n + 1; r < t.length; r++) {
            if (t[n] === t[r]) t.splice(r, 1)
        }
    }
    return t
}

function arrayUniqueCustom(e) {
    var t = e.concat();
    for (var n = 0; n < t.length; n++) {
        for (var r = n + 1; r < t.length; r++) {
            if (t[n].station_train_code === t[r].station_train_code && t[n].targetSeat === t[r].targetSeat) t.splice(r, 1)
        }
    }
    return t
}
var setStorage = function (e, t) {
    var n = e.split("."),
        r = n.length;
    if (r === 0) return;
    if (r === 1) {
        return localStorage.setItem(n[0], JSON.stringify(t))
    }
    var i = JSON.parse(localStorage.getItem(n[0])),
        s = {};
    if (!i || typeof i !== "object") {
        localStorage.setItem(n[0], JSON.stringify({}));
        i = JSON.parse(localStorage.getItem(n[0]))
    }
    s = i;
    for (var o = 1, u = r - 1; o < u; o++) {
        if (!s[n[o]]) {
            s[n[o]] = {}
        }
        s = s[n[o]]
    }
    s[n[r - 1]] = t;
    return localStorage.setItem(n[0], JSON.stringify(i))
};
var getStorage = function (e) {
    if (!e) return null;
    var t = e.split("."),
        n = t.length,
        r = null;
    if (n === 0) return null;
    if (n === 1) {
        var i = null;
        try {
            i = JSON.parse(localStorage.getItem(t[0]))
        } catch (s) {
            i = JSON.parse(JSON.stringify(localStorage.getItem(t[0])))
        }
        return i
    }
    r = JSON.parse(localStorage.getItem(t[0]));
    if (r === null) return null;
    for (var o = 1; o < n; o++) {
        try {
            r = JSON.parse(JSON.stringify(r[t[o]]))
        } catch (s) {
            r = r[t[o]]
        }
    }
    return r
};
var removeStorage = function (e) {
    return localStorage.removeItem(e)
};
var trainTypeClassify = function (e, t) {
    var n = [];
    e.forEach(function (e, t) {
        if (e.match(/^\D+/)) {
            var r = e[0].toUpperCase();
            n.push(r)
        }
    });
    return arrayUnique(n.concat(t))
};
var objFilteAndJoin = function (e, t) {
    var n = e;
    if (e.constructor === Object) {
        n = [];
        for (var r in e) {
            n.push(r)
        }
    }
    return arrayUnique(n.concat(t))
};
var SGTicket = {
    timer: 0,
    timer_speed: 0,
    curIptIdSGTicketx: 0,
    curDayMS: Date.now(),
    curMonth: 0,
    availMonth: 0,
    numOfMonths: 2,
    curUser: "",
    altDates: [],
    fromStation: [],
    toStation: [],
    manualTrainNo: "",
    queryTimes: 0,
    countDownObj: null,
    availRange: 20,
    brush: null,
    runtimeInfos: {
        passenger: {},
        seat: {},
        train: [],
        altDate: {}
    },
    containerMap: {
        passenger: $(".panel_part_passenger .chosen_rets_ul"),
        seat: $(".panel_part_seat .chosen_rets_ul"),
        train: $(".panel_part_train .chosen_rets_ul"),
        altDate: $(".panel_part_altDate .chosen_rets_ul")
    },
    defaultDes: {
        passenger: "请选择乘客",
        seat: "选择越靠前的席别优先级越高",
        train: "选择越靠前的车次优先级越高",
        altDate: "如果您设定的出发日期没有符合的席别和车次，自动查询备选日期的票"
    },
    seatMap: {
        yw: "硬卧",
        ze: "二等座",
        yz: "硬座",
        zy: "一等座",
        rz: "软座",
        rw: "软卧",
        gr: "高级软卧",
        tz: "特等座",
        swz: "商务座",
        wz: "无座"
    },
    seatPriorityRM: ["yw", "ze", "yz", "zy", "rz", "rw", "gr", "tz", "swz", "wz"],
    trainPriorityRM: ["G", "C", "D", "Z", "T", "K", "O"],
    log: function () {
        return;
        console.log.apply(console, arguments)
    },
    postMessage2CS: function (e, t) {
        var n = {
            type: "PAGE",
            cmd: e,
            param: t || null
        };
        return window.postMessage(n, "*")
    },
    beforeInit: function () {
        var e = SGTicket,
            t = 0;
        if (navigator.userAgent.indexOf("SE") <= 0) {
            e.initDialogs();
            if (e.checkSearch("sogou_vr")) {
                e.showInstallSEFromSearch()
            } else {
                e.showInstallSE()
            }
            return
        }
        this.timer = setInterval(function () {
            if (t >= 10) {
                clearInterval(e.timer);
                e.initDialogs();
                e.showInstallExt();
                return
            }
            t++
        }, 100)
    },
    showInstallSE: function () {
        this.showMessage({
            pDes: "请使用搜狗抢票专版",
            btnDes: "下载搜狗抢票专版",
            btnLink: "http://ie.sogou.com/12306/"
        })
    },
    showInstallSEFromSearch: function () {
        this.showMessage({
            pDes: "请使用搜狗抢票专版",
            btnDes: "下载搜狗抢票专版",
            btnLink: "http://ie.sogou.com/12306/?r=search"
        })
    },
    showInstallExt: function () {
        this.showMessage({
            pDes: "使用搜狗加速抢票需要安装搜狗抢票扩展",
            btnDes: "安装搜狗抢票插件",
            btnLink: "http://ie.sogou.com/app/app_4122.html"
        })
    },
    showMessage: function (param) {
        $(".msg_op").addClass("page_hide");
        $("#msg_btn").off("click");
        $(".msg_des_p").html(param.pDes);
        if (param.btnDes) {
            $("#msg_btn").html(param.btnDes).attr("href", param.btnLink);
            $(".msg_op").removeClass("page_hide")
        }
        if (param.btnClickCB) {
            $("#msg_btn").click(function () {
                eval(param.btnClickCB)
            })
        }
        SGTicket.handleClickDialogBtn({
            dialog: $(".d_message")
        })
    },
    onGotLoginRandCode: function (e) {
        var t = e.base64;
        SGTicket.postMessage2CS("OCRLoginRandCode", {
            base64: t
        });
        $("#randcode_img").attr("src", t)
    },
    loginRandCodeOCRManager: function () {
        var e = true;
        return {
            onGotOCRLoginRandCode: function (t) {
                if (!e) return;
                var n = t.code;
                if (!n) {
                    SGTicket.clearOCRLoginTips();
                    return
                }
                $("#randCode").val(n);
                SGTicket.clearOCRLoginTips(true)
            },
            setStatus: function (t) {
                e = t
            }
        }
    }(),
    clearOCRLoginTips: function (e) {
        if (e) {
            $("#login_dialog").addClass("login_fillOCR")
        } else {
            $("#login_dialog").removeClass("login_OCR")
        }
        SGTicket.loginRandCodeOCRManager.setStatus(false)
    },
    queryInfoBegin: function (e) {
        clearInterval(SGTicket.timer_speed);
        SGTicket.timer_speed = setInterval(function () {
            var e = +("" + Math.random()).slice(-3);
            $("#speed_up_vala").html(e)
        }, 300);
        var t = !!e && !!e.param ? e.param.queryInfos : null;
        if (!!t) {
            var n = ("" + Math.abs(t[1] - t[2])).slice(0, 2);
            $("#speed_up_server").html(t[4]);
            $("#speed_up_val").html(n == 0 ? "" : n);
            $("#brush_times").html(++SGTicket.queryTimes)
        }
        if (!$(".d_brush").is(":visible")) {
            SGTicket.handleClickDialogBtn({
                dialog: $(".d_brush")
            });
            SGTicket.countDownObj.start(5);
            $(".brush_con").removeClass("brush_con_after");
            SGTicket.brush.off();
            SGTicket.brush.on()
        }
    },
    addEllipsis: function () {
        $(".limit_w").each(function (e, t) {
            $(t).one("overflowchanged", function () {
                if ($(this).width() >= 182 && $(this).width() < 235) {
                    $(this).parent(".brush_con").addClass("brush_con_after")
                }
            })
        })
    },
    brushAnimation: function () {
        function n() {
            var e = SGTicket.runtimeInfos,
                t = "",
                n = "",
                r = "",
                i = "";
            $(".brush_con_date").text(e.trainDate.split(/\s+/)[0]);
            $(".brush_con_from").text(e.fromStationText[0]);
            $(".brush_con_to").text(e.toStationText[0]);
            e.train.forEach(function (e) {
                t += "<li>" + e + "</li>"
            });
            $(".brush_con_priTrain ul").html(t);
            for (var s in e.seat) {
                n += "<li>" + e.seat[s] + "</li>"
            }
            $(".brush_con_priSeat ul").html(n);
            for (var o in e.altDate) {
                r += "<li>" + e.altDate[o] + "</li>"
            }
            $(".brush_con_altDate ul").html(r);
            for (var u in e.passenger) {
                i += "<li>" + e.passenger[u].split(".")[0] + "</li>"
            }
            $(".brush_con_pass ul").html(i);
            $(".brush_con").removeClass("brush_con_after");
            SGTicket.addEllipsis()
        }
        var e = 0,
            t = 0;
        return {
            on: function () {
                n();
                e = setInterval(function () {
                    if (t >= 8) {
                        t = 0
                    }
                    $(".d_brush_server li").removeClass("highlight").eq(t).addClass("highlight");
                    t++
                }, .3 * 1e3)
            },
            off: function () {
                t = 0;
                clearTimeout(e);
                $(".d_brush_server li").removeClass("highlight")
            }
        }
    }(),
    init: function () {
        $(".content_wrap").removeClass("page_hide");
        this.initDate();
        this.initInputsOP();
        this.initSwitchTab();
        this.initDialogs();
        this.bindEvents();
        this.postMessage2CS("cancelQueryInfoRM");
        this.countDownObj = this.countDown();
        this.countDownObj.stop();
        this.postMessage2CS("change2Webkit");
        this.postMessage2CS("pingback", {
            type: "pageOpen"
        });
        this.brush = SGTicket.brushAnimation
    },
    checkThree: function (e) {
        return !!e.fromStationText && !!e.fromStationText.length && !!e.toStationText && !!e.toStationText.length && !!e.trainDate
    },
    renderLoginData: function (e) {
        var t = SGTicket;
        var n = e.username,
            r = {};
        this.curUser = !!e.username ? e.username : "anonymousUser";
        if (this.checkSearch("sogou_vr")) {
            this.setPrefillFromSearch()
        } else {
            if (!!getStorage("anonymousUser") && !!getStorage("anonymousUser.prefillData") && this.checkThree(getStorage("anonymousUser.prefillData"))) {
                r = getStorage("anonymousUser.prefillData");
                for (var i in r) {
                    this.runtimeInfos[i] = r[i]
                }
                if (!!n) {
                    setStorage(this.curUser, {
                        prefillData: r
                    })
                }
            } else if (!!getStorage(n) && !!getStorage(n + ".prefillData")) {
                r = getStorage(n + ".prefillData");
                for (var i in r) {
                    this.runtimeInfos[i] = r[i]
                }
            } else {
                setStorage(this.curUser, {
                    prefillData: {}
                })
            } if (!!n) {
                this.renderRightSelectedRets("all")
            } else {
                this.renderRightSelectedRets("withoutPassenger")
            }
        } if (!!n) {
            $("#username_head").html(this.curUser);
            $(".login_info").removeClass("page_hide");
            $(".logo_des").removeClass("page_hide");
            $(".bind_mob_wrap").removeClass("page_hide");
            $("#auto_brush").html("自动刷票");
            !!getStorage(n + ".userMobile") && $("#bind_mob").prop("checked", true)
        } else {
            SGTicket.handleClickDialogBtn({
                dialog: $(".d_login"),
                callback: SGTicket.loginHandler
            });
            $("#username_head").html("");
            $(".login_info").addClass("page_hide");
            $(".logo_des").addClass("page_hide")
        }
        setStorage("curUser", this.curUser);
        setTimeout(function () {
            t.revertStations();
            t.canQueryTicket()
        }, 30)
    },
    loginHandler: function () {
        SGTicket.postMessage2CS("getLoginRandCode");
        $("#login_dialog").removeClass("login_fillOCR").addClass("login_OCR");
        SGTicket.loginRandCodeOCRManager.setStatus(true)
    },
    initInputsOP: function () {
        var e = SGTicket;
        var t = $(".station_input");
        t.click(function () {
            e.postMessage2CS("pickFromStation")
        });
        t.each(function (t, n) {
            $(this).on("input", function (n) {
                if (!$.trim($(this).val()).length) {
                    $(".dialog").fadeOut(100);
                    e.curIptIdx = t;
                    $(".station_input").eq(t).trigger("click")
                } else {
                    $(".dialog").not($("#station_box_1")).fadeOut(100);
                    e.postMessage2CS("searchStation", {
                        keyword: $(this).val().toLowerCase(),
                        iptIdx: e.curIptIdx
                    })
                }
            })
        })
    },
    initSwitchTab: function () {
        var e = SGTicket;
        $(".station_title_li").each(function (t, n) {
            $(n).click(function (t) {
                t.stopPropagation();
                $(".dialog").not($("#station_box_0")).fadeOut(100);
                $(".station_title_li").removeClass("selected");
                $(this).addClass("selected");
                e.postMessage2CS("renderStationsBox", {
                    tabIdx: $(this).index(),
                    pageIdx: 0,
                    iptIdx: e.curIptIdx
                })
            })
        });
        $(".paging_prev").click(function () {
            e.postMessage2CS("renderStationsBox", {
                tabIdx: +this.dataset.tabidx,
                pageIdx: +this.dataset.pageidx
            })
        });
        $(".paging_next").click(function () {
            e.postMessage2CS("renderStationsBox", {
                tabIdx: +this.dataset.tabidx,
                pageIdx: +this.dataset.pageidx
            })
        });
        $(".search_paging_prev").click(function () {
            e.postMessage2CS("renderStationsSearch", {
                pageIdx: +this.dataset.pageidx
            })
        });
        $(".search_paging_next").click(function () {
            e.postMessage2CS("renderStationsSearch", {
                pageIdx: +this.dataset.pageidx
            })
        });
        $(".station_content_ul").on("click", "li", function (t) {
            t.stopPropagation();
            var n = 1;
            var r = {},
                i = e.curIptIdx === 0 ? "fromStationText" : "toStationText";
            r[i] = [$(this).attr("title"), $(this).data("scode")];
            if (getStorage(e.curUser + ".prefillData." + i) && getStorage(e.curUser + ".prefillData." + i)[0] === r[i][0]) {
                n = 0
            }
            setStorage(e.curUser + ".prefillData." + i, r[i]);
            setStorage("anonymousUser.prefillData." + i, r[i]);
            e.runtimeInfos[i] = r[i];
            $("#station_box_0").fadeToggle(100);
            e.afterChangeStation(n)
        });
        $(".station_search_ul").on("click", "li", function (t) {
            t.stopPropagation();
            var n = 1;
            var r = {},
                i = e.curIptIdx === 0 ? "fromStationText" : "toStationText";
            r[i] = [$(this).attr("title"), $(this).data("scode")];
            if (getStorage(e.curUser + ".prefillData." + i) && getStorage(e.curUser + ".prefillData." + i)[0] === r[i][0]) {
                n = 0
            }
            setStorage(e.curUser + ".prefillData." + i, r[i]);
            setStorage("anonymousUser.prefillData." + i, r[i]);
            e.runtimeInfos[i] = r[i];
            $("#station_box_1").fadeToggle(100);
            e.afterChangeStation(n)
        })
    },
    initDate: function () {
        function t(e) {
            var t = $("#datepicker"),
                n = SGTicket.curDayMS = e[0],
                r = SGTicket.availRange = e[1],
                i = 24 * 60 * 60 * 1e3;
            SGTicket.curMonth = (new Date(n)).getMonth() + 1;
            SGTicket.availMonth = (new Date(n + r * i)).getMonth() + 1;
            SGTicket.numOfMonths = SGTicket.availMonth - SGTicket.curMonth < 0 ? 12 - SGTicket.curMonth + SGTicket.availMonth + 1 : SGTicket.availMonth - SGTicket.curMonth + 1;
            var s = {
                regional: $.datepicker.regional["zh-CN"],
                dateFormat: "yy-mm-dd  D",
                numberOfMonths: [1, SGTicket.numOfMonths < 2 ? 2 : SGTicket.numOfMonths],
                minDate: new Date(n),
                maxDate: new Date(n + (r - 1) * i)
            };
            t.datepicker(s);
            t.datepicker("option", "showOptions", {
                direction: "up"
            });
            SGTicket.generateAltDate()
        }
        window.standardtime = function () {
            t(arguments);
            setStorage(this.curUser + ".prefillData.trainDate", $("#datepicker").val())
        };
        var e = document.createElement("script");
        e.type = "text/javascript";
        e.src = "http://12306.ie.sogou.com/mm_12306/time.php?t=" + (new Date).getTime();
        document.getElementsByTagName("body")[0].appendChild(e)
    },
    initDialogs: function () {
        var e = SGTicket;
        var t = {
            autoOpen: false,
            show: {
                effect: "blind",
                duration: 100
            },
            hide: {
                effect: "blind",
                duration: 100
            },
            draggable: false,
            resizable: false,
            title: null,
            modal: false
        };
        $(".d_login").dialog($.extend({}, t, {
            dialogClass: "dialog_popup dialog_login",
            width: 550,
            height: 366,
            position: {
                my: "center",
                at: "center",
                of: window
            },
            modal: true,
            draggable: true
        }));
        $(".d_seat").dialog($.extend({}, t, {
            dialogClass: "dialog_filter dialog_notitlebar",
            width: 370,
            height: 165,
            position: {
                my: "left top",
                at: "left bottom+5",
                of: $("#choose_seat_btn")
            }
        }));
        $(".d_train").dialog($.extend({}, t, {
            dialogClass: "dialog_filter dialog_notitlebar",
            width: 320,
            maxHeight: 362,
            position: {
                my: "left top",
                at: "left bottom+5",
                of: $("#choose_train_btn")
            }
        }));
        $(".d_passenger").dialog($.extend({}, t, {
            dialogClass: "dialog_filter dialog_notitlebar",
            width: 400,
            position: {
                my: "left top",
                at: "left bottom+5",
                of: $("#choose_passenger_btn")
            }
        }));
        $(".d_altDate").dialog($.extend({}, t, {
            dialogClass: "dialog_filter dialog_notitlebar",
            width: 500,
            minHeight: 48,
            position: {
                my: "left top",
                at: "left bottom+5",
                of: $("#choose_altDate_btn")
            }
        }));
        $(".d_target").dialog($.extend({}, t, {
            dialogClass: "dialog_popup dialog_target",
            width: 510,
            height: 314,
            title: "提交订单",
            position: {
                my: "center",
                at: "center",
                of: window
            },
            modal: true,
            draggable: true,
            closeOnEscape: false,
            close: function (t, n) {
                !!t.handleObj && e.postMessage2CS("cancel");
                e.postMessage2CS("cancelQueryInfoRM");
                SGTicket.brush.off()
            }
        }));
        $(".d_message").dialog($.extend({}, t, {
            dialogClass: "dialog_popup dialog_message",
            width: 510,
            title: "搜狗抢票4.0",
            position: {
                my: "center",
                at: "center",
                of: window
            },
            modal: true,
            draggable: true,
            closeOnEscape: false,
            close: function (t, n) {
                e.postMessage2CS("cancelQueryInfoRM");
                SGTicket.brush.off()
            }
        }));
        $(".d_passenger_popup").dialog($.extend({}, t, {
            dialogClass: "dialog_popup dialog_popup_passenger",
            width: 510,
            title: "请选择乘车人",
            position: {
                my: "center",
                at: "center",
                of: window
            },
            modal: true,
            draggable: true,
            closeOnEscape: false
        }));
        $(".d_brush").dialog($.extend({}, t, {
            dialogClass: "dialog_popup dialog_brush",
            width: 980,
            height: 608,
            position: {
                my: "center",
                at: "center",
                of: window
            },
            modal: true,
            draggable: true,
            closeOnEscape: false,
            close: function (t, n) {
                SGTicket.queryTimes = 0;
                $("#brush_times").html(0);
                $("#speed_up_server").html(0);
                $("#speed_up_val").html(0);
                $(".RM_ul").html("");
                $(".suggStation_ul").html("");
                $(".sugg_wrap").addClass("page_hide");
                $(".RM_wrap").addClass("loadingRM");
                !!t.handleObj && e.postMessage2CS("cancel");
                e.postMessage2CS("cancelQueryInfoRM");
                SGTicket.brush.off();
                SGTicket.countDownObj.stop();
                !!t.handleObj && e.postMessage2CS("pingback", {
                    type: "cancelBrush"
                })
            }
        }))
    },
    afterChangeStation: function (e) {
        var t = this;
        this.revertStations();
        if (!!e) {
            this.canQueryTicket();
            this.runtimeInfos.train = [];
            setStorage(t.curUser + ".prefillData.train", []);
            setStorage("anonymousUser.prefillData.train", []);
            this.renderRightSelectedRets("train")
        }
    },
    handleClickDialogBtn: function (e) {
        var t = e.dialog,
            n = e.isToggle,
            r = e.callback;
        if (n) {
            if (!t.dialog("isOpen")) {
                $(document).trigger("click");
                t.dialog("open")
            } else {
                t.dialog("close")
            }
        } else {
            $(document).trigger("click");
            t.dialog("open")
        } if (r) r()
    },
    clearAllDialogs: function () {
        $(".d_seat").dialog("close");
        $(".d_train").dialog("close");
        $(".d_passenger").dialog("close");
        $(".d_altDate").dialog("close");
        $(".d_login").dialog("close");
        $(".d_target").dialog("close");
        $(".d_brush").dialog("close");
        $(".d_passenger_popup").dialog("close");
        $(".d_message").dialog("close")
    },
    generateAltDate: function () {
        var e = 24 * 60 * 60 * 1e3,
            t = SGTicket.availRange,
            n = 0,
            r = 0,
            i = 0,
            s = 0;
        for (var o = 0; o < t; o++) {
            s = this.curDayMS + o * e;
            n = (new Date(s)).getFullYear();
            r = (new Date(s)).getMonth() + 1 - 10 >= 0 ? (new Date(s)).getMonth() + 1 : "0" + ((new Date(s)).getMonth() + 1);
            i = (new Date(s)).getDate() - 10 >= 0 ? (new Date(s)).getDate() : "0" + (new Date(s)).getDate();
            this.altDates.push(n + "-" + r + "-" + i)
        }
    },
    setPrefillData: function () {
        var e = getStorage("prefillData");
        this.revertStations();
        if (!e || !Object.keys(e).length) return;
        this.fromStation = e.fromStationText || [];
        this.toStation = e.toStationText || [];
        this.runtimeInfos.passenger = e.selectedPassengers || {};
        this.runtimeInfos.seat = e.selectedSeats || {};
        this.runtimeInfos.train = e.selectedTrains || {};
        this.runtimeInfos.altDate = e.selectedAltDates || {};
        this.renderRightSelectedRets("all")
    },
    setPrefillFromSearch: function () {
        var e = decodeURIComponent(location.search.replace("?", "")).split("&"),
            t = {},
            n = {};
        e.forEach(function (e) {
            var n = e.split("=");
            if (n[1]) {
                t[n[0]] = n[1]
            }
        });
        if (t.fromStation) {
            n.fromStationText = [t.fromStation, t.fromStationCode]
        }
        if (t.toStation) {
            n.toStationText = [t.toStation, t.toStationCode]
        }
        if (t.trainDate) {
            n.trainDate = t.trainDate
        }
        if (t.t) {
            n.t = t.t
        }
        if (getStorage(this.curUser + ".prefillData.t") === t.t) {
            var r = getStorage(this.curUser + ".prefillData");
            for (var i in r) {
                this.runtimeInfos[i] = r[i]
            }
            this.renderRightSelectedRets("all");
            return
        }
        if (!Object.keys(n).length) return;
        setStorage(this.curUser + ".prefillData", n)
    },
    checkSearch: function (e) {
        return !!location.search && location.search.indexOf(e) >= 0
    },
    revertStations: function () {
        var e = $("#fromStationText"),
            t = $("#toStationText");
        var n = getStorage(this.curUser + ".prefillData.fromStationText"),
            r = getStorage(this.curUser + ".prefillData.toStationText"),
            i = "",
            s = "",
            o = "",
            u = "";
        if (getStorage(this.curUser + ".prefillData.trainDate") && !$("#datepicker").val()) {
            $("#datepicker").datepicker("setDate", new Date(getStorage(this.curUser + ".prefillData.trainDate").split(/\s+/)[0]))
        } else {
            setStorage(this.curUser + ".prefillData.trainDate", $("#datepicker").val())
        } if (n instanceof Array) {
            i = n[0];
            s = n[1]
        } else {
            i = "简拼/全拼/汉字"
        } if (r instanceof Array) {
            o = r[0];
            u = r[1]
        } else {
            o = "简拼/全拼/汉字"
        }
        e.val(i);
        e.get(0).dataset.scode = s;
        t.val(o);
        t.get(0).dataset.scode = u
    },
    renderDefaultDes: function (e) {
        var t = "<li>" + this.defaultDes[e] + "</li>";
        this.containerMap[e].html(t)
    },
    renderRightSelectedRets: function (e) {
        function s(e, n, r) {
            if (!n) return;
            if (!e || !Object.keys(e).length) {
                t.renderDefaultDes(r)
            } else {
                var i = "";
                if (e.constructor === Array) {
                    for (var s = 0, o = e.length; s < o; s++) {
                        i += '<li class="chosen_rets_li" data-type="' + e[s].split(".")[0] + '">' + e[s].split(".")[0] + "</li>"
                    }
                } else {
                    for (var u in e) {
                        i += '<li class="chosen_rets_li" data-type="' + u + '" data-full="' + e[u] + '">' + e[u].split(".")[0] + "</li>"
                    }
                }
                SGTicket.log("container: ", n);
                n.html(i)
            }
        }
        var t = SGTicket,
            n = null,
            r = null;
        switch (e) {
        case "passenger":
            s(t.runtimeInfos.passenger, t.containerMap.passenger, "passenger");
            break;
        case "seat":
            s(t.runtimeInfos.seat, t.containerMap.seat, "seat");
            break;
        case "train":
            s(t.runtimeInfos.train, t.containerMap.train, "train");
            break;
        case "altDate":
            s(t.runtimeInfos.altDate, t.containerMap.altDate, "altDate");
            break;
        case "all":
            for (var i in t.runtimeInfos) {
                s(t.runtimeInfos[i], t.containerMap[i], i)
            }
            break;
        case "withoutPassenger":
            for (var i in t.runtimeInfos) {
                if (i === "passenger") continue;
                s(t.runtimeInfos[i], t.containerMap[i], i)
            }
            break;
        default:
            break
        }
    },
    renderSelectedPassengerRets: function () {
        if (!Object.keys(this.selectedPassengers).length) {
            this.renderDefaultPassengerDes()
        } else {
            var e = "";
            for (var t in this.selectedPassengers) {
                e += '<li class="chosen_rets_li" data-type="' + t + '">' + this.selectedSeats[t] + "</li>"
            }
            $(".panel_part_passenger").html(e)
        }
    },
    modifySelected: function (e, t, n) {
        if (t === -1) {
            e.children().remove()
        }
        e.children().eq(t).remove()
    },
    canQueryTicket: function () {
        var e = $("#fromStationText").get(0).dataset.scode,
            t = $("#toStationText").get(0).dataset.scode,
            n = $("#datepicker").val().split(/\s+/g)[0];
        if (!!e && !!t) {
            this.postMessage2CS("queryLeftTicket", {
                fromStationCode: e,
                toStationCode: t,
                date: n
            })
        }
    },
    verifyLoginInfo: function () {
        var e = $("#username").val(),
            t = $("#password").val(),
            n = $("#randCode").val();
        if (e.trim().length === 0) {
            this.showLoginErr("登录名必须填写!", "username");
            return false
        }
        if (t.trim().length < 6) {
            this.showLoginErr("密码必须填写,且不少于6位", "password");
            return false
        }
        if (n.trim().length < 4) {
            this.showLoginErr("验证码长度为4位!", "randCode");
            $("#refresh_randcode").trigger("click");
            return false
        }
        return true
    },
    showLoginErr: function (e, t) {
        $("#error_msg").html(e);
        t && $("#" + t + "").addClass("err")
    },
    hideLoginErr: function () {
        $("#error_msg").html("");
        $(".login_form input").removeClass("err")
    },
    sortTrain: function (e, t) {
        $(".sort_btn").removeClass("sort_active");
        $(".sort_btn").not(e).removeClass("sort_desc");
        e.addClass("sort_active");
        e.toggleClass("sort_desc");
        if (e.hasClass("sort_desc")) {
            e.addClass("sort_active");
            this.postMessage2CS("sortTrainList", {
                sortBy: t,
                order: "desc"
            })
        } else {
            this.postMessage2CS("sortTrainList", {
                sortBy: t,
                order: "asc"
            })
        }
    },
    filterWithCondition: function (e, t) {
        var n = objFilteAndJoin(SGTicket.runtimeInfos.seat, SGTicket.seatPriorityRM),
            r = SGTicket.seatPriorityRM,
            i = objFilteAndJoin(trainTypeClassify(SGTicket.runtimeInfos.train, SGTicket.trainPriorityRM), SGTicket.trainPriorityRM),
            s = [],
            o = SGTicket.runtimeInfos.train;
        var u = Object.keys(SGTicket.runtimeInfos.seat).length;
        if (!!u) {
            r = n.slice(0, u)
        }
        switch (t) {
        case "originCon":
            var a = [],
                f = [];
            if (!!o.length) {
                e && e.forEach(function (e) {
                    o.forEach(function (t) {
                        if (t === e.queryLeftNewDTO.station_train_code) {
                            n.forEach(function (t) {
                                var n = JSON.parse(JSON.stringify(e));
                                if (n.buttonTextInfo.indexOf("预订") >= 0 && n.queryLeftNewDTO[t + "_num"] !== "--" && n.queryLeftNewDTO[t + "_num"] !== "无") {
                                    n.queryLeftNewDTO.targetSeat = t;
                                    a.push(n.queryLeftNewDTO)
                                }
                            })
                        }
                    })
                })
            } else {
                e && e.forEach(function (e) {
                    i.forEach(function (t) {
                        if (t === e.queryLeftNewDTO.station_train_code[0].toUpperCase()) {
                            n.forEach(function (t) {
                                var n = JSON.parse(JSON.stringify(e));
                                if (n.buttonTextInfo.indexOf("预订") >= 0 && n.queryLeftNewDTO[t + "_num"] !== "--" && n.queryLeftNewDTO[t + "_num"] !== "无") {
                                    n.queryLeftNewDTO.targetSeat = t;
                                    a.push(n.queryLeftNewDTO)
                                }
                            })
                        }
                    })
                })
            }
            e && e.forEach(function (e) {
                i.forEach(function (t) {
                    if (t === e.queryLeftNewDTO.station_train_code[0].toUpperCase()) {
                        r.forEach(function (t) {
                            var n = JSON.parse(JSON.stringify(e));
                            if (n.buttonTextInfo.indexOf("预订") >= 0 && n.queryLeftNewDTO[t + "_num"] !== "--" && n.queryLeftNewDTO[t + "_num"] !== "无") {
                                n.queryLeftNewDTO.targetSeat = t;
                                f.push(n.queryLeftNewDTO)
                            }
                        })
                    }
                })
            });
            SGTicket.log("retDataBySeatPri: ", a);
            SGTicket.log("retDataByTrainPri: ", f);
            SGTicket.log(a.slice(0, 5).concat(f.slice(0, 5)));
            SGTicket.log(arrayUniqueCustom(a.slice(0, 5).concat(f.slice(0, 5))));
            this.renderRMList(arrayUniqueCustom(a.slice(0, 5).concat(f.slice(0, 5))));
            break;
        default:
            break
        }
    },
    renderRMList: function (e) {
        var t = this;
        if (!e || !e.length) return;
        if (!$(".d_brush").is(":visible")) {
            t.postMessage2CS("cancelQueryInfoRM");
            return
        }
        var n = $(".RM_ul"),
            r = $(".RM_wrap"),
            i = "";
        e.forEach(function (e) {
            var n = SGTicket.runtimeInfos.trainDate.split(/\s+/)[0];
            var r = t.analysisPrice(e.yp_info);
            var s = SGTicket.runtimeInfos.train.some(function (t) {
                    if (t === e.station_train_code) {
                        return true
                    }
                }) ? "" : "diff",
                o = 1;
            for (var u in SGTicket.runtimeInfos.seat) {
                if (e.targetSeat === u) {
                    o = 0
                }
            }
            o = !!o ? "diff" : "";
            i += '<li><span class="RM_train_no ' + s + '">' + e.station_train_code + '</span><span class="RM_train_date">' + n + "</span>" + '<span class="RM_train_time">' + e.start_time + "-" + e.arrive_time + "</span>" + '<span class="RM_train_station">' + e.from_station_name + "-" + e.to_station_name + "</span>" + '<span class="RM_train_seat ' + o + '">' + SGTicket.seatMap[e.targetSeat] + "(" + e[e.targetSeat + "_num"] + ")</span>" + '<span class="RM_train_price">￥' + r[e.targetSeat + "_pr"] + "</span>" + '<span class="RM_train_buyBtn manualBuy">立即购买</span></li>'
        });
        r.removeClass("loadingRM");
        n.html(i)
    },
    analysisPrice: function (e) {
        if (!e || !e.length) return;
        var t = 10,
            n = e.length / t,
            r = "",
            i = "",
            s = {};
        for (var o = 0; o < n; o++) {
            r = e.substr(o * t, t);
            i = r.substr(1, 5) / 10;
            switch (r[0]) {
            case "9":
                s.swz_pr = i;
                break;
            case "P":
                s.tz_pr = i;
                break;
            case "M":
                s.zy_pr = i;
                break;
            case "O":
                s.ze_pr = s.wz_pr = i;
                break;
            case "6":
                s.gr_pr = i;
                break;
            case "4":
                s.rw_pr = i;
                break;
            case "3":
                s.yw_pr = i;
                break;
            case "2":
                s.rz_pr = i;
                break;
            case "1":
                s.yz_pr = s.wz_pr = i;
                break;
            default:
                break
            }
        }
        return s
    },
    countDown: function () {
        var e = 0,
            t = this;
        return {
            start: function (n) {
                var r = origin = n;
                e = setInterval(function () {
                    if (r <= 0) {
                        r = origin;
                        t.postMessage2CS("queryInfoRM");
                        $(".RM_ul").html("");
                        $(".suggStation_ul").html("");
                        $(".sugg_wrap").addClass("page_hide");
                        $(".RM_wrap").addClass("loadingRM")
                    }
                    $("#RM_cd").html(--r)
                }, 1e3)
            },
            stop: function () {
                clearInterval(e);
                $("#RM_cd").html(0)
            }
        }
    },
    renderSuggStation: function (e) {
        function t(e) {
            $(".sugg_from").html(e.originalFromStationName);
            $(".sugg_to").html(e.originalToStationName);
            $(".sugg_mid").html(e.toStationName)
        }

        function n(e) {
            $(".sugg_from").html(e.originalFromStationName);
            $(".sugg_to").html(e.toStationName);
            $(".sugg_mid").html(e.originalToStationName)
        }
        var r = e.data,
            i = r[0];
        var s = $(".suggStation_ul"),
            o = "",
            u = "before",
            a = "",
            f = "";
        if (r[0].stationStatus === "before") {
            $(".hint_type").removeClass("hint_type_before hint_type_after").addClass("hint_type_before");
            t(i)
        } else {
            u = "after";
            $(".hint_type").removeClass("hint_type_before hint_type_after").addClass("hint_type_after");
            n(i)
        }
        $(".des_to").html(i.originalToStationName);
        $(".des_to_buy").html(i.toStationName);
        r.forEach(function (e) {
            if (u === "before") {
                a = '<span class="s_station s_from" data-fromcode="' + e.originalFromStationCode + '">' + e.originalFromStationName + "</span>" + '<span class="s_station">-</span>' + '<span class="s_station s_to"  data-tocode="' + e.toStationCode + '">' + e.toStationName + "</span>" + '<span class="s_station">-(</span>' + '<span class="s_station">' + e.originalToStationName + "</span>" + '<span class="s_station">)</span>' + '<span class="s_station hint_icon"></span>';
                f = e.originalFromStationName + "-" + e.toStationName + "-(" + e.originalToStationName + ")"
            } else {
                a = '<span class="s_station s_from" data-fromcode="' + e.originalFromStationCode + '">' + e.originalFromStationName + "</span>" + '<span class="s_station">-(</span>' + '<span class="s_station">' + e.originalToStationName + "</span>" + '<span class="s_station">)-</span>' + '<span class="s_station s_to"  data-tocode="' + e.toStationCode + '">' + e.toStationName + "</span>" + '<span class="s_station hint_icon"></span>';
                f = e.originalFromStationName + "-(" + e.originalToStationName + ")-" + e.toStationName
            }
            o += "<li>" + '<span class="RM_train_no">' + e.train + "</span>" + '<span class="RM_train_date">' + e.startTrainDate + "</span>" + '<span class="RM_train_time">' + e.startTime + "-" + e.arriveTime + "</span>" + '<span class="RM_train_station Sugg_train_station diff" title="' + f + '">' + a + "</span>" + '<span class="RM_train_seat Sugg_train_seat">' + SGTicket.seatMap[e.seat] + "(" + e.seatNum + ")</span>" + '<span class="RM_train_price">￥' + e.seatPrice + "</span>" + '<span class="RM_train_buyBtn manualBuy">立即购买</span>' + "</li>"
        });
        s.html(o);
        $(".RM_wrap").removeClass("loadingRM");
        $(".sugg_wrap").removeClass("page_hide")
    },
    manualBuyCB: function (e, t) {
        var n = this;
        if (!Object.keys(n.runtimeInfos.passenger).length) {
            n.postMessage2CS("getPassengers", {
                manualBuy: true
            });
            $("#pass_confirm_btn").addClass("disable")
        } else {
            $("#pass_confirm_btn").removeClass("disable");
            n.passConfirmCB(t)
        }
        n.postMessage2CS("pingback", {
            type: "manuallyBuy"
        })
    },
    passConfirmCB: function (e) {
        var t = this;
        if ($("#pass_confirm_btn").hasClass("disable")) return;
        if (e) {
            t.postMessage2CS("manualBrush", {
                trainNo: t.manualTrainNo,
                seat: t.manualSeat,
                date: $("#queryDate").text(),
                startStationText: t.manualFromName,
                startStation: t.manualFromCode,
                toStationText: t.manualToName,
                toStation: t.manualToCode,
                isSuggestQuery: true
            })
        } else {
            t.postMessage2CS("manualBrush", {
                trainNo: t.manualTrainNo,
                seat: t.manualSeat,
                date: $("#queryDate").text()
            })
        }
        t.postMessage2CS("cancelQueryInfoRM")
    },
    bindEvents: function () {
        var e = SGTicket;
        $("#logout_btn").on("click", function () {
            e.postMessage2CS("logout")
        });
        $(".station_input").each(function (t, n) {
            $(n).on("click focus", function (n) {
                n.stopPropagation();
                if ($("#station_box_1").css("display") !== "none" && e.curIptIdx === t) return;
                e.curIptIdx = t;
                $(".station_input").removeClass("curStation");
                $(this).addClass("curStation");
                $(".station_title_li").eq(0).trigger("click");
                e.clearAllDialogs();
                if (n.type === "click") {
                    setTimeout(function () {
                        n.target.select()
                    }, 50)
                }
            });
            $(n).on("blur", function (t) {
                SGTicket.log("blur");
                e.revertStations()
            })
        });
        $("#change_station").click(function (t) {
            var n = $("#fromStationText"),
                r = $("#toStationText");
            var i = r.val(),
                s = r.get(0).dataset.scode;
            r.val(n.val());
            r.get(0).dataset.scode = n.get(0).dataset.scode;
            n.val(i);
            n.get(0).dataset.scode = s;
            setStorage(e.curUser + ".prefillData.fromStationText", [n.val(), n.get(0).dataset.scode]);
            setStorage(e.curUser + ".prefillData.toStationText", [r.val(), r.get(0).dataset.scode]);
            setTimeout(function () {
                e.canQueryTicket();
                e.runtimeInfos.train = [];
                setStorage(e.curUser + ".prefillData.train", []);
                setStorage("anonymousUser.prefillData.train", []);
                e.renderRightSelectedRets("train")
            }, 0)
        });
        var t = $("#datepicker");
        t.on("click focus", function () {
            var e = $("#ui-datepicker-div");
            e.css("top", "138px");
            setTimeout(function () {
                !$(".datepicker_insert_tips").get(0) && $('<p class="datepicker_insert_tips">可订票日期</p>  <p class="datepicker_insert_tips page_hide">仅售临客车票</p>').appendTo(e)
            }, 10)
        });
        t.on("change", function () {
            setStorage(e.curUser + ".prefillData.trainDate", t.val());
            setStorage("anonymousUser.prefillData.trainDate", t.val());
            SGTicket.runtimeInfos.trainDate = t.val();
            e.canQueryTicket()
        });
        t.on("focus", function () {
            $(".dialog").fadeOut(100)
        });
        $("#auto_brush").on("click", function () {
            if (getStorage("curUser") === "anonymousUser") {
                $("#randcode_img").trigger("click");
                $(".d_login").parents(".ui-dialog").css("position", "fixed");
                e.handleClickDialogBtn({
                    dialog: $(".d_login"),
                    callback: e.loginHandler
                })
            } else {
                e.clearAllDialogs();
                e.postMessage2CS("autoBrush");
                e.queryInfoBegin()
            }
            e.postMessage2CS("pingback", {
                type: "autoBuy"
            })
        });
        $("#login_btn").on("click", function () {
            e.clearOCRLoginTips();
            if (!!e.verifyLoginInfo()) {
                e.postMessage2CS("login", [$("#username").val(), $("#password").val(), $("#randCode").val()]);
                $("#randCode").val("")
            }
        });
        $("#randCode").on("click focus", function () {
            e.clearOCRLoginTips()
        });
        $("#manual_login").on("click focus", function () {
            e.clearOCRLoginTips()
        });
        $("#choose_seat_btn").on("click", function (t) {
            t.stopPropagation();
            $(".seat_panel_ul input").each(function (t, n) {
                $(n).prop("checked", false);
                if ($(n).attr("id") in e.runtimeInfos.seat) {
                    $(n).prop("checked", true)
                }
            });
            e.handleClickDialogBtn({
                dialog: $(".d_seat"),
                isToggle: true
            })
        });
        $("#choose_train_btn").on("click", function (t) {
            t.stopPropagation();
            e.postMessage2CS("renderTrainListDialog");
            e.handleClickDialogBtn({
                dialog: $(".d_train"),
                isToggle: true
            })
        });
        $("#choose_passenger_btn").on("click", function (t) {
            t.stopPropagation();
            e.postMessage2CS("getPassengers", {
                clicked: true
            })
        });
        $("#choose_altDate_btn").on("click", function (t) {
            t.stopPropagation();
            var n = "",
                r = "";
            SGTicket.log("self.altDates", e.altDates.length);
            for (var i = 0, s = e.altDates.length; i < s; i++) {
                if (!!e.runtimeInfos.altDate[e.altDates[i]]) {
                    r = "checked"
                }
                n += '<li><input id="' + e.altDates[i] + '" class="list_altDate_ipt" type="checkbox" ' + r + " />" + '<label for="' + e.altDates[i] + '" class="list_altDate_label"><span>' + e.altDates[i] + "</span></label></li>";
                r = ""
            }
            $(".altDate_panel_ul").html(n);
            e.handleClickDialogBtn({
                dialog: $(".d_altDate"),
                isToggle: true
            })
        });
        $(".no_sort_btn").on("click", function () {
            e.sortTrain($(this), "tNo")
        });
        $(".time_sort_btn").on("click", function () {
            e.sortTrain($(this), "tTime")
        });
        $(".duration_sort_btn").on("click", function () {
            e.sortTrain($(this), "tDuration")
        });
        $("#randcode_img").on("click", function () {
            e.clearOCRLoginTips();
            $(this).attr("src", $(this).data("src") + Date.now());
            $("#randCode").val("")
        });
        $("#refresh_randcode").on("click", function () {
            e.clearOCRLoginTips();
            $("#randcode_img").trigger("click")
        });
        $("#order_rc_img").on("click", function () {
            $(this).attr("src", $(this).data("src") + Date.now())
        });
        $("#refresh_order_rc").on("click", function () {
            $("#order_rc_img").trigger("click")
        });
        $(window).on("keydown", function (t) {
            SGTicket.log("keyCode: ", t.keyCode);
            if ($("#station_box_1").css("display") === "block") {
                if (t.keyCode === 40) {
                    t.preventDefault();
                    e.postMessage2CS("goDown")
                } else if (t.keyCode === 38) {
                    t.preventDefault();
                    e.postMessage2CS("goUp")
                } else if (t.keyCode === 13) {
                    $(".station_search_ul > li.cur").trigger("click")
                }
            }
        });
        $(".login_form").not($("#password")).on("keydown", function (e) {
            if (e.keyCode === 13) {
                $("#login_btn").trigger("click")
            }
        });
        $(document).on("click", function (t) {
            if ($(".ui-draggable").is(":visible") && t.target.tagName) {
                if (t.target.tagName === "BODY" || t.target.tagName === "HTML" || t.target.className && t.target.className.indexOf("ui-widget-overlay") >= 0 || t.target.className.indexOf("ui-draggable") >= 0) {
                    t.stopPropagation();
                    return
                }
            }
            $(".dialog").fadeOut(100);
            e.clearAllDialogs();
            e.revertStations()
        });
        $("#station_box_0, #station_box_1, .ui-dialog-titlebar, .d_train, .d_seat, .d_passenger, .d_altDate, #auto_brush, .d_login, .d_target, .d_brush, .d_passenger_popup, .d_message").on("click", function (e) {
            e.stopPropagation()
        });
        $(".passenger_panel_ul, .passenger_popup_ul").on("click", "input", function () {
            var t = $(this),
                n = t.prop("checked"),
                r = $("input[data-name=" + t.data("name") + "]");
            if (!n) {
                delete e.runtimeInfos.passenger[t.attr("id")]
            } else {
                if (e.runtimeInfos.passenger && Object.keys(e.runtimeInfos.passenger).length >= 5) {
                    t.prop("checked", false);
                    $(".msg_des_p").html("最多添加5位联系人");
                    $(".msg_op").addClass("page_hide");
                    e.handleClickDialogBtn({
                        dialog: $(".d_message")
                    });
                    return
                }
                e.runtimeInfos.passenger[t.attr("id")] = t.data("name") + "." + t.data("typename") + "." + t.attr("id")
            }
            r.each(function (e, t) {
                t.checked = n
            });
            if (e.runtimeInfos.passenger && !!Object.keys(e.runtimeInfos.passenger).length) {
                $("#pass_confirm_btn").removeClass("disable")
            } else {
                $("#pass_confirm_btn").addClass("disable")
            }
            e.renderRightSelectedRets("passenger");
            setStorage(e.curUser + ".prefillData.passenger", e.runtimeInfos.passenger);
            setStorage("anonymousUser.prefillData.passenger", e.runtimeInfos.passenger)
        });
        $(".seat_panel_ul").on("click", "input", function () {
            var t = $(this);
            if (!t.prop("checked")) {
                t.removeClass("selecteItem");
                delete e.runtimeInfos.seat[t.attr("id")]
            } else {
                if (e.runtimeInfos.seat && Object.keys(e.runtimeInfos.seat).length >= 5) {
                    t.prop("checked", false);
                    $(".msg_des_p").html("最多添加5个优先席位");
                    $(".msg_op").addClass("page_hide");
                    e.handleClickDialogBtn({
                        dialog: $(".d_message")
                    });
                    return
                }
                t.addClass("selecteItem");
                e.runtimeInfos.seat[t.attr("id")] = t.next().text()
            }
            e.renderRightSelectedRets("seat");
            setStorage(e.curUser + ".prefillData.seat", e.runtimeInfos.seat);
            setStorage("anonymousUser.prefillData.seat", e.runtimeInfos.seat);
            e.postMessage2CS("renderTrainTable")
        });
        $(".train_panel_ul").on("click", "input", function () {
            var t = $(this);
            if (!t.prop("checked")) {
                t.removeClass("selecteItem");
                e.runtimeInfos.train.splice(e.runtimeInfos.train.indexOf(t.attr("id")), 1)
            } else {
                if (e.runtimeInfos.train && Object.keys(e.runtimeInfos.train).length >= 5) {
                    t.prop("checked", false);
                    $(".msg_des_p").html("最多添加5个优先车次");
                    $(".msg_op").addClass("page_hide");
                    e.handleClickDialogBtn({
                        dialog: $(".d_message")
                    });
                    return
                }
                t.addClass("selecteItem");
                e.runtimeInfos.train.push(t.next().find(".train_no").text())
            }
            e.renderRightSelectedRets("train");
            setStorage(e.curUser + ".prefillData.train", e.runtimeInfos.train);
            setStorage("anonymousUser.prefillData.train", e.runtimeInfos.train);
            e.postMessage2CS("renderTrainTable")
        });
        $(".altDate_panel_ul").on("click", "input", function () {
            var t = $(this);
            if (!t.prop("checked")) {
                t.removeClass("selecteItem");
                delete e.runtimeInfos.altDate[t.attr("id")]
            } else {
                if (e.runtimeInfos.altDate && Object.keys(e.runtimeInfos.altDate).length >= 5) {
                    t.prop("checked", false);
                    $(".msg_des_p").html("最多添加5个备选日期");
                    $(".msg_op").addClass("page_hide");
                    e.handleClickDialogBtn({
                        dialog: $(".d_message")
                    });
                    return
                }
                t.addClass("selecteItem");
                e.runtimeInfos.altDate[t.attr("id")] = t.next().text()
            }
            e.renderRightSelectedRets("altDate")
        });
        $(".panel_part_passenger .panel_part_right").on("click", "li", function () {
            delete e.runtimeInfos.passenger[$(this).data("type")];
            setStorage(e.curUser + ".prefillData.passenger", e.runtimeInfos.passenger);
            setStorage("anonymousUser.prefillData.passenger", e.runtimeInfos.passenger);
            e.renderRightSelectedRets("passenger")
        });
        $(".panel_part_seat .panel_part_right").on("click", "li", function () {
            delete e.runtimeInfos.seat[$(this).data("type")];
            setStorage(e.curUser + ".prefillData.seat", e.runtimeInfos.seat);
            setStorage("anonymousUser.prefillData.seat", e.runtimeInfos.seat);
            e.renderRightSelectedRets("seat");
            e.postMessage2CS("renderTrainTable")
        });
        $(".panel_part_train .panel_part_right").on("click", "li", function () {
            e.runtimeInfos.train.splice($(this).index(), 1);
            setStorage(e.curUser + ".prefillData.train", e.runtimeInfos.train);
            setStorage("anonymousUser.prefillData.train", e.runtimeInfos.train);
            e.renderRightSelectedRets("train");
            e.postMessage2CS("renderTrainTable")
        });
        $(".panel_part_altDate .panel_part_right").on("click", "li", function () {
            delete e.runtimeInfos.altDate[$(this).data("type")];
            e.renderRightSelectedRets("altDate")
        });
        $("#change_to_manual").on("click", function () {
            e.postMessage2CS("stopAutoOCR");
            $("#order_rc_input").val("").focus();
            setTimeout(function () {
                $("#order_rc_img").trigger("click");
                $(".auto_rc").addClass("page_hide");
                $(".manual_rc").removeClass("page_hide");
                $("#make_order").removeClass("disable")
            }, 2 * 1e3)
        });
        $("#make_order").on("click", function () {
            if ($(this).hasClass("disable")) return;
            if ($("#order_rc_input").val().length < 4) {
                $("#order_err").html("验证码长度不足4位");
                return
            }
            e.postMessage2CS("makeOrder", {
                randCode: $("#order_rc_input").val()
            });
            $("#order_err").html("");
            $(this).addClass("disable")
        });
        $("#order_rc_input").on("keyup", function () {
            if ($(this).val().length >= 4) {
                $("#make_order").trigger("click");
                return
            }
        });
        $("#order_rc_input").on("click", function () {
            $("#order_err").html("")
        });
        $("#train_info_panel").on("click", ".manualBuy", function (t) {
            e.manualTrainNo = $(t.currentTarget).parents("tr").find("label:eq(0)").text();
            e.manualSeat = $(t.currentTarget).find("p span:eq(0)").text();
            e.manualBuyCB(t)
        });
        $(".RM_ul").on("click", ".manualBuy", function (t) {
            e.manualTrainNo = $(t.currentTarget).parents("li").find(".RM_train_no").text();
            e.manualSeat = $(t.currentTarget).parents("li").find(".RM_train_seat").text().split("(")[0];
            $(t.currentTarget).addClass("buying");
            e.manualBuyCB(t)
        });
        $(".suggStation_ul").on("click", ".manualBuy", function (t) {
            var n = $(t.currentTarget).parents("li");
            e.manualTrainNo = n.find(".RM_train_no").text();
            e.manualSeat = n.find(".RM_train_seat").text().split("(")[0];
            e.manualFromName = n.find(".s_from").text();
            e.manualFromCode = n.find(".s_from").data("fromcode");
            e.manualToName = n.find(".s_to").text();
            e.manualToCode = n.find(".s_to").data("tocode");
            $(t.currentTarget).addClass("buying");
            e.manualBuyCB(t, true)
        });
        $("#pass_confirm_btn").on("click", function () {
            e.passConfirmCB()
        });
        $("#fold_btn").on("click", function () {
            $(this).hide();
            $(".filter_wrap").animate({
                height: 0
            }, 300, "swing", function () {
                $("#unfold_btn").show();
                $(".query_rets").toggleClass("fold")
            })
        });
        $("#unfold_btn").on("click", function () {
            $(this).hide();
            $(".query_rets").toggleClass("fold");
            $(".filter_wrap").animate({
                height: 270
            }, 300, "swing", function () {
                $("#fold_btn").show()
            })
        });
        $(".self_scroll").on("mousewheel", function (e) {
            var t = $(this),
                n = this.scrollTop,
                r = this.scrollHeight,
                i = t.height(),
                s = e.originalEvent.wheelDelta,
                o = s > 0;
            var u = function () {
                e.stopPropagation();
                e.preventDefault();
                e.returnValue = false;
                return false
            };
            if (!o && -s > r - i - n) {
                t.scrollTop(r);
                return u()
            } else if (o && s > n) {
                t.scrollTop(0);
                return u()
            }
        });
        $(window).on("scroll", function () {
            if (window.scrollY < 500) {
                $(".top_btn").hide();
                return
            }
            $(".top_btn").show()
        });
        $(".top_btn").on("click", function () {
            window.scrollTo(0, 0)
        });
        $(".login_form").on("click", "input", function (t) {
            e.hideLoginErr();
            t.target.select()
        });
        $("#auto_keyin_randcode").on("click", function () {
            if ($(this).prop("checked")) {
                SGTicket.postMessage2CS("startAutoOCR")
            } else {
                SGTicket.postMessage2CS("stopAutoOCR")
            }
        });
        var n = $("#bind_mob").on("click", function (t) {
            var n = "";
            if ($(this).prop("checked")) {
                e.postMessage2CS("getUserMobile");
                setTimeout(function () {
                    if (!!getStorage(SGTicket.curUser + ".userMobile")) {
                        n = getStorage(SGTicket.curUser + ".userMobile")
                    }
                    e.showMessage({
                        pDes: "已成功绑定手机" + (n ? "<br>绑定手机号：" + n : ""),
                        btnDes: "确定",
                        btnLink: 'javascript:$(document).trigger("click")'
                    });
                    e.postMessage2CS("pingback", {
                        type: "bindMobile",
                        mob: n
                    })
                }, 200)
            } else {
                setTimeout(function () {
                    setStorage(SGTicket.curUser + ".userMobile", "");
                    e.postMessage2CS("pingback", {
                        type: "unbindMobile"
                    })
                }, 0)
            }
        });
        SGTicket.addEllipsis();
        $(".brush_conditions").on({
            mouseenter: function () {
                $(".twinkle_div").removeClass("on");
                if (!$(".brush_con").hasClass("brush_con_after")) {
                    return
                }
                $(".brush_con").removeClass("brush_con_after");
                $(".brush_con_header").addClass("showmore");
                $(".brush_con_main").addClass("showmore");
                $(".limit_w").addClass("showmore")
            },
            mouseleave: function () {
                $(".twinkle_div").addClass("on");
                $(".brush_con_header").removeClass("showmore");
                $(".brush_con_main").removeClass("showmore");
                $(".limit_w").removeClass("showmore");
                SGTicket.addEllipsis()
            }
        });
        $(".prebook_btn").on("click", function () {
            SGTicket.postMessage2CS("pingback", {
                type: "click_btn_prebook"
            })
        });
        $(document).on("keydown", ".dialog_notitlebar, .dialog_target, .dialog_brush, .dialog_message, .dialog_popup_passenger", function (e) {
            if (e.keyCode === 32) {
                e.stopPropagation();
                e.preventDefault();
                return
            }
        })
    }
};
SGTicket.beforeInit();
window.addEventListener("message", function (e) {
    if (e.source != window) return;
    if (e.data.type && e.data.type == "ContentScript") {
        SGTicket.log("------page postMessage listener: ", e.data.cmd);
        var t = e.data.cmd,
            n = e.data.param;
        switch (t) {
        case "extInstalled":
            clearInterval(SGTicket.timer);
            SGTicket.init();
            break;
        case "logoutFailFromCS":
        case "getUsernameFailFromCS":
            SGTicket.clearAllDialogs();
            SGTicket.renderLoginData(n);
            SGTicket.postMessage2CS("cancel");
            SGTicket.postMessage2CS("cancelQueryInfoRM");
            SGTicket.brush.off();
            SGTicket.countDownObj.stop();
            break;
        case "loginSuccFromCS":
        case "getUsernameSuccFromCS":
            SGTicket.clearAllDialogs();
            SGTicket.renderLoginData(n);
            setTimeout(function () {
                var e = $("#unfold_btn");
                !!e.is(":visible") && e.trigger("click")
            }, 100);
            break;
        case "getPassengersFailFromCS":
            SGTicket.handleClickDialogBtn({
                dialog: $(".d_login"),
                callback: SGTicket.loginHandler
            });
            break;
        case "getPassengersSuccFromCS":
            !!n.clicked && SGTicket.handleClickDialogBtn({
                dialog: $(".d_passenger"),
                isToggle: true
            });
            if (!!n.manualBuy) {
                !!Object.keys(SGTicket.runtimeInfos.passenger).length && $("#pass_confirm_btn").removeClass("disable");
                SGTicket.handleClickDialogBtn({
                    dialog: $(".d_passenger_popup")
                })
            }
            var r = n.passengers || [],
                i = SGTicket.runtimeInfos.passenger || {},
                s = {};
            if (!Object.keys(r).length) {
                i = {};
                SGTicket.renderDefaultDes("passenger")
            } else {
                if (!!Object.keys(i).length) {
                    for (var o in i) {
                        for (var u = 0, a = r.length; u < a; u++) {
                            if (r[u].passenger_id_no === o) {
                                s[o] = i[o]
                            }
                        }
                    }
                    SGTicket.runtimeInfos.passenger = s;
                    SGTicket.renderRightSelectedRets("all")
                }
            }
            break;
        case "queryInfoBegin":
            SGTicket.queryInfoBegin(e.data);
            break;
        case "showTargetTrainDialog":
            if ($(".d_message").is(":visible")) return;
            if (!!$("#auto_keyin_randcode").prop("checked")) {
                SGTicket.postMessage2CS("autoOCR");
                $(".auto_rc").removeClass("page_hide");
                $(".manual_rc").addClass("page_hide");
                $("#make_order").addClass("disable")
            } else {
                $("#change_to_manual").trigger("click")
            }
            SGTicket.handleClickDialogBtn({
                dialog: $(".d_target")
            });
            break;
        case "orderRCFail":
            SGTicket.log("order rc fail");
            if ($(".d_message").is(":visible")) return;
            setTimeout(function () {
                $("#order_rc_img").trigger("click");
                $("#order_rc_input").val("");
                $("#make_order").removeClass("disable")
            }, 2 * 1e3);
            SGTicket.handleClickDialogBtn({
                dialog: $(".d_target")
            });
            break;
        case "gotTicket":
            SGTicket.showMessage({
                pDes: n.pDes,
                btnDes: n.btnDes,
                btnLink: n.btnLink
            });
            break;
        case "waitTicket":
            SGTicket.showMessage({
                pDes: n.pDes
            });
            break;
        case "buyingTimeException":
        case "passengerException":
        case "stationException":
        case "hasUnpaidOrder":
            SGTicket.showMessage({
                pDes: n.pDes,
                btnDes: n.btnDes || null,
                btnLink: n.btnLink || null
            });
            break;
        case "autoOCRFail":
            if ($(".d_message").is(":visible")) return;
            $("#change_to_manual").trigger("click");
            SGTicket.handleClickDialogBtn({
                dialog: $(".d_target")
            });
            break;
        case "errorTicketTime":
            setTimeout(function () {
                SGTicket.showMessage({
                    pDes: n.pDes
                })
            }, 200);
            break;
        case "confirmQueueFail":
            SGTicket.showMessage(n);
            break;
        case "gotLoginRandCode":
            SGTicket.onGotLoginRandCode(n);
            break;
        case "gotOCRLoginRandCode":
            SGTicket.loginRandCodeOCRManager.onGotOCRLoginRandCode(n);
            break;
        case "getUserMobileSucc":
            setStorage(SGTicket.curUser + ".userMobile", n.userMobile);
            break;
        case "loginErrFromBg":
            var f = n.errMsg;
            if (f.indexOf("登录名") >= 0) {
                SGTicket.showLoginErr(f, "username")
            } else if (f.indexOf("密码") >= 0) {
                SGTicket.showLoginErr(f, "password")
            } else if (f.indexOf("验证码") >= 0 || f.indexOf("randCodeError") >= 0) {
                var l = f === "randCodeError" ? "验证码错误" : f;
                SGTicket.showLoginErr(l, "randCode")
            } else {
                SGTicket.showLoginErr(f, "")
            }
            break;
        case "getleftTicketRM":
            SGTicket.filterWithCondition(n.leftTicketRMData, n.type);
            break;
        case "suggestStation":
            SGTicket.renderSuggStation(n);
            break;
        default:
            break
        }
    }
});